package com.xr.miniprogram.controller.comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.common.constant.OrderStatusConstants;
import com.xr.common.constant.StaffConstants;
import com.xr.common.constant.WorkStatusConstants;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.miniprogram.domain.MiniProgramOrder;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramServiceLog;
import com.xr.miniprogram.domain.vo.MiniProgramServiceLogVO;
import com.xr.miniprogram.service.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author da
 * @since 2023-09-28
 */
@RestController
@RequestMapping("/miniprogram/serviceLog")
public class MiniProgramServiceLogController extends BaseController {
    @Resource
    private IMiniProgramServiceLogService serviceLogService;
    @Resource
    private IMiniProgramOrderUserService orderUserService;
    @Resource
    private IMiniProgramOrderService orderService;
    @Resource
    private IMiniProgramStaffService staffService;


    /**
     * 根据orderUserId查询相应的服务日志
     * @param orderUserId
     * @return
     */
    @RequestMapping("/get/{orderUserId}")
    public AjaxResult get(@PathVariable("orderUserId") Long orderUserId){
        return success(serviceLogService.getServiceLogPageVOByOrderUserId(orderUserId));
    }

    /**
     * 修改服务日志包括添加服务图片，改变工作状态
     * @param msfStaffServiceLog
     * @return
     */
    @Transactional
    @RequestMapping("/update")
    public AjaxResult update(@RequestBody MiniProgramServiceLogVO msfStaffServiceLog){
        logger.debug(String.valueOf(msfStaffServiceLog));
        Long workStatus = msfStaffServiceLog.getWorkStatus();
        //当工作状态变为2时，即开始服务，更新orderUser状态为服务中
        if(workStatus.equals(WorkStatusConstants.IN_SERVICE)){
            Long serviceLogId = msfStaffServiceLog.getServiceLogId();
            MiniProgramOrderUser orderUser = orderUserService.getByServiceLogId(serviceLogId);
            orderUser.setOrderStatusId(OrderStatusConstants.SERVICE_ORDER);//修改当前服务订单为服务中

            orderUserService.updateById(orderUser);
        }
        //当工作状态变为3时，即服务完成，更新order表状态,若没有其他orderUser改为待评价、orderUser表为待评价状态、以及staff无需要服务的订单改为未分配状态
        if(workStatus.equals(WorkStatusConstants.END_SERVICE)){
            Long serviceLogId = msfStaffServiceLog.getServiceLogId();
            MiniProgramOrderUser orderUser = orderUserService.getByServiceLogId(serviceLogId);
            MiniProgramOrder order = orderService.getById(orderUser.getOrderId());
            orderUser.setOrderStatusId(OrderStatusConstants.EVALUATED_ORDER);//修改当前服务订单为待评价

            int orderUserCount = orderUserService.count(new QueryWrapper<MiniProgramOrderUser>().lambda()//orderUser中 状态为 待评价、待完成 之外的=1
                    .eq(MiniProgramOrderUser::getOrderId, order.getOrderId())
                    .ne(MiniProgramOrderUser::getOrderStatusId, OrderStatusConstants.EVALUATED_ORDER)
                    .ne(MiniProgramOrderUser::getOrderStatusId, OrderStatusConstants.COMPLETION_ORDER));
            if(orderUserCount == 1){
                order.setOrderStatusId(OrderStatusConstants.EVALUATED_ORDER);
            }

            Long staffId = msfStaffServiceLog.getStaffId();
            int count = serviceLogService.count(new QueryWrapper<MiniProgramServiceLog>().lambda().eq(MiniProgramServiceLog::getStaffId , staffId).ne(MiniProgramServiceLog::getWorkStatus , WorkStatusConstants.END_SERVICE));
            if(count == 1){
                staffService.updateStaffStatus(staffId, StaffConstants.UNALLOCATED);
            }

            orderUserService.updateById(orderUser);
            orderService.updateById(order);
        }
        return success(serviceLogService.updateById(MiniProgramServiceLogVO.toMiniProgramServiceLog(msfStaffServiceLog)));
    }
}
